<?php

class DT_SHOP {
	public $shop_config = '';

	function __construct(){
		$this->shop_config = variable_get('dt_shop_config');
	}

	function dt_shop_categories() {
		$data = '';
		$field_categories = $this->shop_config['shop-categories-default'];
		if($field_categories != '') {
			$field_categories = explode('#', $field_categories);
			$vocabulary = taxonomy_vocabulary_machine_name_load($field_categories[0]);
			$data = taxonomy_get_tree($vocabulary->vid);
		}

		return $data;
	}

	function dt_shop_portfolio() {
		$filter = array();
		$grid = array();
		$load_more = '';
		$type = "shop";
		$field_filter = $this->shop_config['shop-categories-front'];
		$items = $this->shop_config['shop-items-portfolio'];
		if($items == 0) {
			$items = 1;
		}
		$load_more = $this->shop_config['shop-load-more'];
		//load filter
		if($field_filter != '') {
			$field_filter = explode('#', $field_filter);
			$vocabulary = taxonomy_vocabulary_machine_name_load($field_filter[0]);
			$terms = entity_load('taxonomy_term', FALSE, array('vid' => $vocabulary->vid));
			if(count($terms) != 0) {
				$filter = $terms;
			}
		}
		//load gird items
		if($filter != '') {
			foreach($filter as $tid => $term) {
				$query = new EntityFieldQuery();
				$entities = $query->entityCondition('entity_type', 'node')
					->propertyCondition('type', $type)
					->propertyOrderBy('nid', 'DESC')
					->propertyCondition('status', NODE_PUBLISHED)
					->fieldCondition($field_filter[1], 'tid', $tid)
					->pager($items) // this is the bit you're missing
					->execute();
				$nodes = (count($entities) != 0) ? node_load_multiple(array_keys($entities['node'])) : null;
        if($nodes != null) {
          $nodes = node_view_multiple($nodes,'teaser');
          array_pop($nodes['nodes']);
          $grid[$tid] = $nodes['nodes'];
        }

			}
		}

		$data['filter'] = $filter;
		$data['grid'] = $grid;
		$data['load-more'] = $load_more;

		return $data;
	}

	function dt_shop_favorite() {
		$data = array();
		$type = 'shop';
		$items = $this->shop_config['shop-items-favorite'];
		if($items == 0) {
			$items = 1;
		}
		$query = new EntityFieldQuery();
		$entities = $query->entityCondition('entity_type', 'node')
			->propertyCondition('type', $type)
			->propertyOrderBy('nid', 'DESC')
			->propertyCondition('status', NODE_PUBLISHED)
			->fieldCondition('field_shop_favorite', 'value', 'yes')
			->range(0,$items) // this is the bit you're missing
			->execute();
		$nodes = (count($entities) != 0) ? node_load_multiple(array_keys($entities['node'])) : null;
		if($nodes != null) {
      $nodes = node_view_multiple($nodes,'teaser');
      array_pop($nodes['nodes']);
			$data = $nodes['nodes'];
		}
		return $data;
	}

	function dt_shop_same($nid) {
		// dsm($nid);
		$data = array();
		$type = 'shop';
		$items = $this->shop_config['shop-items-same'];
		$field_categories = $this->shop_config['shop-categories-default'];
		if($items == 0) {
			$items = 1;
		}
		$query = new EntityFieldQuery();
		$query->entityCondition('entity_type', 'node')
		 ->propertyCondition('type', $type)
		 ->propertyOrderBy('nid', 'DESC')
		 ->propertyCondition('status', NODE_PUBLISHED);
		if($nid == -1) {
			$query->pager(1); // this is the bit you're missing
		} else {
			$query->propertyCondition('nid', $nid);
		}
		$entities = $query->execute();
		$node_sourse = (count($entities) != 0) ? node_load_multiple(array_keys($entities['node'])) : false;
		if(($node_sourse != false) && ($field_categories != '')){
			//load node sourse
			$node_sourse = array_shift($node_sourse);
			//get categories default
			$field_categories = explode('#', $field_categories);
			//get term id by node
			$tid = $node_sourse->$field_categories[1];
			$tid = $tid[LANGUAGE_NONE][0]['tid'];

			// [1][LANGUAGE_NONE][0]['tid'];
			unset($query);
			unset($entities);
			$query = new EntityFieldQuery();
			$entities = $query->entityCondition('entity_type', 'node')
			  ->propertyCondition('type', $type)
			  ->propertyOrderBy('nid', 'DESC')
			  ->propertyCondition('status', NODE_PUBLISHED)
			  ->fieldCondition($field_categories[1], 'tid', $tid)
			  ->range(0,$items) // this is the bit you're missing
			  ->execute();
			$nodes = (count($entities) != 0) ? node_load_multiple(array_keys($entities['node'])) : null;
			if($nodes != null) {
				$nodes = node_view_multiple($nodes,'teaser');
        array_pop($nodes['nodes']);
        $data = $nodes['nodes'];
			}
		}

		return $data;
		// }
	}
}

function dt_shop_block_info() {//display prtb in block
	$blocks['shop-categories'] = array(
		'info' => t('Shop : categories'),
		'cache' => DRUPAL_CACHE_GLOBAL
	);
	$blocks['shop-portfolio'] = array(
		'info' => t('Shop : portfolio'),
		'cache' => DRUPAL_CACHE_GLOBAL
	);
	$blocks['shop-product-favorite'] = array(
		'info' => t('Shop : favorite'),
		'cache' => DRUPAL_CACHE_GLOBAL
	);
	$blocks['shop-product-same'] = array(
		'info' => t('Shop : related'),
		'cache' => DRUPAL_CACHE_GLOBAL
	);

	return $blocks;
}

function dt_shop_block_view($delta = "") {
	$path = drupal_get_path('module', 'dt_shop');
	$block = array();
	$shop_config = variable_get('dt_shop_config');
	$dt_shop = new DT_SHOP();
	$user_data = $_SESSION['user_data'];
	switch ($delta) {
		case 'shop-categories': {
			$dt_data['data'] = $dt_shop->dt_shop_categories();
			$dt_data['user_data'] = $user_data;
			$block['content'] = theme('shop_categories', array('dt_data' => $dt_data));
		}break;
		case 'shop-portfolio': {
			if($shop_config['shop-load-more'] == 'yes') {
				drupal_add_js($path . '/js/dt-shop-load-more.js');
			}
			$block['content'] = theme('shop_portfolio', array('dt_data' => $dt_shop->dt_shop_portfolio()));
		}break;
		case 'shop-product-favorite': {
			$block['content'] = theme('shop_product_favorite', array('dt_data' => $dt_shop->dt_shop_favorite()));
		}break;
		case 'shop-product-same': {
			$dt_data['user_data'] = $user_data;
			$dt_data['data'] = $dt_shop->dt_shop_same($dt_data['user_data']['nid']);
			$block['content'] = theme('shop_product_same', array('dt_data' => $dt_data));
		}break;
	}

	return $block;
}

